//
//  KZExt_String.swift
//  Fanrengame
//
//  Created by 朱凯 on 2018/4/23.
//  Copyright © 2018年 JooKey. All rights reserved.
//

import UIKit

// MARK: ------ 对字符串进行宽度高度等直接获得的拓展
extension String {
    
    /// 单行：根据字体的大小获得文本的size
    func size(fontSize: CGFloat) -> CGSize {
        
        let str = self as NSString
        let attributes = [NSAttributedStringKey.font: kFont(fontSize)]
        return str.size(withAttributes: attributes)
    }
    
    /// 单行：根据字体的大小获得文本的宽度
    func width(fontSize: CGFloat) -> CGFloat {
        
        return self.size(fontSize: fontSize).width + 2
    }
    
    /// 多行：根据字体的大小和可适应的size获得rect
    func rect(size: CGSize, fontSize: CGFloat) -> CGRect {
        
        let str = self as NSString
        let attributes = [NSAttributedStringKey.font: kFont(fontSize)]
        return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
    }
    
    /// 多行：根据字体的大小和可适应的size获得文本高度
    func height(size: CGSize, fontSize: CGFloat) -> CGFloat {
        
        return rect(size: size, fontSize: fontSize).size.height + 2
    }
}


// MARK: ------ 富文本
extension String {

    
    /// 根据当前字符串和输出参数返回富文本字符串
    ///
    /// - Parameters:
    ///   - attrubutes: 富文本字典
    ///   - scope: 需要改变的字符串
    /// - Returns: 富文本字符串
    func attrubutesString(attrubutes: [NSAttributedStringKey: Any], scope: String) -> NSAttributedString {
        
        let result = NSMutableAttributedString.init(string: self)
        let range = NSRange.init(location: self.index(of: scope.first!)!.encodedOffset, length: Int(scope.count))
        result.setAttributes(attrubutes, range: range)
        return result.copy() as! NSAttributedString
    }
    /// 根据当前字符串和输出参数返回富文本字符串
    ///
    /// - Parameters:
    ///   - attrubutes: 富文本字典
    ///   - scopeRange: 需要改变的字符串的位置
    /// - Returns: 富文本字符串
    func attrubutesString(attrubutes: [NSAttributedStringKey: Any], scopeRange: Range<Int>) -> NSAttributedString {

        let result = NSMutableAttributedString.init(string: self)
        let range = NSRange.init(location: scopeRange.lowerBound, length: scopeRange.count)
        result.setAttributes(attrubutes, range: range)
        return result.copy() as! NSAttributedString
    }
    
    
    /// 对本身全部做富文本处理
    ///
    /// - Parameter attrubutes: 富文本字典
    /// - Returns: 富文本字符串
    func attrubutesString(attrubutes: [NSAttributedStringKey: Any]) -> NSAttributedString {
        return attrubutesString(attrubutes: attrubutes, scope: self)
    }
}


// MARK: ------ 自定义一个字符串值转换的协议（用于转成Int等其他基础类型值）
protocol StringValueConvertible {
    
    var intValue: Int {get set}
    var floatValue: Float {get set}
}
extension String : StringValueConvertible {
    
    var intValue: Int {
        get {
            if let value = Int(self) {
                return value
            } else {
                return 0
            }
        }
        set {
            self = "\(newValue)"
        }
    }
    var floatValue: Float {
        get {
            if let value = Float(self) {
                return value
            } else {
                return 0
            }
        }
        set {
            self = "\(newValue)"
        }
    }
}


// MARK: ------ MD5
extension String {
    
    /// 当前字符串的md5加密后的字符串
    var md5: String {
        var digest = [UInt8].init(repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        
        if let data = self.data(using: .utf8) {
            data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Void in
                CC_MD5(bytes, CC_LONG(data.count), &digest)
            }
        }
        
        var digestHex = ""
        for index in 0..<Int(CC_MD5_DIGEST_LENGTH) {
            digestHex += String(format: "%02x", digest[index])
        }
        
        return digestHex
    }
}
